const app = getApp()
const myRequest = require('../../../utils/request'); // 导入整个模块
Page({
  data: {
    year: 0, //记录年
    month: 0, //记录月
    calendarData: [], //记录本周日期
    weekdays: ['一', '二', '三', '四', '五', '六', '日'],
    nowyear: 0,
    nowmonth: 0,
    nowday: 0,
    clickyear: 0,
    clickmonth: 0,
    clickday: 0,
    grouplist: [],
    planlist: [],
    bottomInset: 0,
    username: '',
  },
  getdata() {
    let that = this
    this.setData({
      username: JSON.parse(wx.getStorageSync('userinfo')).username
    })
    myRequest.get('/project/projectdata',{username:this.data.username}).then(res=>{
      let list = []
      res.data.forEach(val => {
        val.projectlist = JSON.parse(val.projectlist)
        let date = val.date.split('/')
        if (date[0] == that.data.clickyear && date[1] == that.data.clickmonth && date[2] == that.data.clickday) {
          list.push(val)
        }
      })
      that.setData({
        grouplist: res.data,
        planlist: list
      })
      that.renderCalendar()
    })
   
  },
  onLoad: function () {
    const systemInfo = wx.getWindowInfo();
    const bottomInset = Math.abs(systemInfo.safeArea.bottom - systemInfo.windowHeight);
    
    this.setData({
      bottomInset: bottomInset + 20,
      nowyear: new Date().getFullYear(),
      nowmonth: new Date().getMonth() + 1,
      nowday: new Date().getDate(),
      clickyear: new Date().getFullYear(),
      clickmonth: new Date().getMonth() + 1,
      clickday: new Date().getDate()
    })

  },
  onShow() {
    this.getdata()

  },
  //切换到上个月 bv
  prevMonth: function () {
    //获取当前年月
    const currentYear = this.data.year
    const currentMonth = this.data.month
    // 计算上个月的年份和月份
    const prevYear = currentMonth === 1 ? currentYear - 1 : currentYear;
    const prevMonth = currentMonth === 1 ? 12 : currentMonth - 1;
    // 更新数据并重新渲染日历
    this.setData({
      year: prevYear,
      month: prevMonth,
    }, this.renderCalendar);
  },

  //切换到下个月
  nextMonth: function () {
    //获取当前年月
    const currentYear = this.data.year
    const currentMonth = this.data.month
    // 计算上个月的年份和月份
    const nextYear = currentMonth === 12 ? currentYear + 1 : currentYear;
    const nextMonth = currentMonth === 12 ? 1 : currentMonth + 1;
    // 更新数据并重新渲染日历
    this.setData({
      year: nextYear,
      month: nextMonth,
    }, this.renderCalendar);

  },

  renderCalendar: function () {
    // 获取日期
    const currentDate = new Date();
    // 获取年月，也用于更新上一个月或下一个月
    const year = this.data.year || currentDate.getFullYear();
    const month = this.data.month || currentDate.getMonth() + 1;
    // 获取当前月份的总天数和第一天是星期几
    const totaldays = new Date(year, month, 0).getDate();
    let firstdayOfWeek = new Date(year, month - 1, 1).getDay();
    if (firstdayOfWeek === 0) {
      firstdayOfWeek = 7;
    }
    // 定义存放日历数据的数组
    const calendarData = [];
    // 初始化一个当前星期的数组，用于存放当前星期的日期
    let currentWeek = [];
  
    // 在当前星期数组中填充空白格子，直到达到当前月份的第一天
    for (let i = 0; i < firstdayOfWeek - 1; i++) { // 设置以周一开始
      currentWeek.push({ day: 0, isactive: false }); // 使用零填充空缺
    }
    // 遍历当前月份的每一天
    for (let i = 1; i <= totaldays; i++) {
      // 格式化日期为 'YYYY/M/D' 形式，不包含前导零
      const dateStr = `${year}/${month}/${i}`;
      const isActive = this.data.grouplist.some(item => {
        // console.log(111,dateStr);
        // 假设 grouplist 中的日期格式是 'YYYY/M/D'
        return item.date === dateStr;
      });
      console.log(isActive);
      currentWeek.push({ day: i, isactive: isActive });
      // 如果当前星期数组长度为 7，表示已经包含一个星期的日期，将其添加到日历数据数组中，并初始化一个新的星期数组
      if (currentWeek.length === 7) {
        calendarData.push(currentWeek);
        currentWeek = [];
      }
    }
  
    // 如果当前星期数组长度大于 0，表示最后一周没有满一行，补齐空白格子，然后将其添加到日历数据数组中
    if (currentWeek.length > 0) {
      for (let i = currentWeek.length; i < 7; i++) {
        currentWeek.push({ day: 0, isactive: false });
      }
      calendarData.push(currentWeek);
    }
  
    // 将生成的日历数据更新到页面数据中，使页面重新渲染
    this.setData({
      year: year,
      month: month,
      calendarData: calendarData,
    });
  },
  selectDate(e) {
    console.log(e);
    this.setData({
      clickday: e.currentTarget.dataset.day,
      clickyear: this.data.year,
      clickmonth: this.data.month
    })
    let list = []
    this.data.grouplist.forEach(val => {
      let date = val.date.split('/')
      if (date[0] == this.data.clickyear && date[1] == this.data.clickmonth && date[2] == this.data.clickday) {
        list.push(val)
      }
    })
    this.setData({

      planlist: list
    })
  },
  //添加规划 
  add() {
    let date = ''
    date = this.data.clickyear + '/' + this.data.clickmonth + '/' + this.data.clickday

    wx.navigateTo({
      url: '/packagesA/pages/plan/addplan/index?date=' + date,
    })
  },
  delete(e) {
    let that = this
    myRequest.post('/project/delete',{
      data: e.currentTarget.dataset.id
    }).then(res=>{
        wx.showToast({
          title: '删除成功！',
          icon:'success'
        })
        that.getdata()
    })
    
  },

  clickbox(e) {
    console.log(e);
    wx.navigateTo({
      url: '/packagesA/pages/plan/addplan/index?item=' + JSON.stringify(e.currentTarget.dataset.item),
    })
  }
});